package com.huluobo.mvvmdemo2006a.view;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.DividerItemDecoration;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.material.snackbar.Snackbar;
import com.huluobo.mvvmdemo2006a.R;
import com.huluobo.mvvmdemo2006a.adapter.ShopListAdapter;
import com.huluobo.mvvmdemo2006a.databinding.FragmentShopBinding;
import com.huluobo.mvvmdemo2006a.model.Product;
import com.huluobo.mvvmdemo2006a.viewmodels.ShopViewModel;

import java.util.List;

/**
 * 商品首页页面
 */
public class ShopFragment extends Fragment implements ShopListAdapter.ShopInterface {

    FragmentShopBinding fragmentShopBinding;
    private ShopViewModel shopViewModel;
    private ShopListAdapter shopListAdapter;
    private NavController navController;

    public ShopFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        fragmentShopBinding = FragmentShopBinding.inflate(inflater, container, false);
        return fragmentShopBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        navController = Navigation.findNavController(view);

        //给页面设置适配器
        shopListAdapter = new ShopListAdapter(this);
        fragmentShopBinding.shopRecycleView.setAdapter(shopListAdapter);
        fragmentShopBinding.shopRecycleView.addItemDecoration(new DividerItemDecoration(requireContext(), DividerItemDecoration.VERTICAL));
        fragmentShopBinding.shopRecycleView.addItemDecoration(new DividerItemDecoration(requireContext(), DividerItemDecoration.HORIZONTAL));

        //通过ViewModelProvider来拿到shopViewModel,并不是直接new出来的对象
        //requireActivity() 获取当前Fragment处于哪个Activity之上,能拿到对应的Activity
        //Owner 代表宿主,也就是所有的ViewModel都需要有个宿主对象
        shopViewModel = new ViewModelProvider(requireActivity()).get(ShopViewModel.class);

        //通过ViewModel获取Repo数据源的方法进行观察,当LiveData的数据发生改变时,onChanged方法会产生回调
        shopViewModel.getProducts().observe(getViewLifecycleOwner(), new Observer<List<Product>>() {
            @Override
            public void onChanged(List<Product> products) {
                //给Adapter赋值,传递List数据源
                shopListAdapter.submitList(products);
            }
        });
    }

    @Override
    public void addItem(Product product) {
        boolean isAdded = shopViewModel.addProductToCart(product);
        if (isAdded) {
            Snackbar.make(requireView(), product.getName() + "商品已添加", Snackbar.LENGTH_LONG).setAction("跳转查看", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    navController.navigate(R.id.action_shopFragment_to_cartFragment);
                }
            }).show();
        } else {
            Snackbar.make(requireView(), "商品已经在购物车中添加满了", Snackbar.LENGTH_LONG).show();
        }
    }

    @Override
    public void onItemClick(Product product) {
        Log.i("TagA", "点击了item:" + product.getName());
        shopViewModel.setProduct(product);
        navController.navigate(R.id.action_shopFragment_to_productFragment);
    }
}